package com.liqi.frame.comm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.liqi.frame.comm.dto.SmsResultDto;
import com.liqi.frame.comm.enums.SmsTypeEnum;
import com.liqi.frame.comm.properties.SmsProperties;
import com.liqi.frame.comm.service.SmsService;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

/**
 * @author liqi on 2020/3/5 0005. liqiwork@qq.com
 */
public class SmsServiceImpl implements SmsService {
    @Autowired
    SmsProperties smsProperties;

    @Override
    public SmsResultDto sendSms(String phone, String[] content, SmsTypeEnum smsTypeEnum) {
        SmsResultDto smsResultDto = new SmsResultDto();
        try {
            String[] params = content;
            SmsSingleSender ssender = new SmsSingleSender(smsProperties.getAppid(), smsProperties.getAppkey());
            SmsSingleSenderResult result = ssender.sendWithParam("86", phone,
                    smsTypeEnum.getKey(), params, smsProperties.getSmsSign(), "", "");  // 签名不能为空串
            BeanUtil.copyProperties(result, smsResultDto);
        } catch (HTTPException e) {
            // HTTP响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // json解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络IO错误
            e.printStackTrace();
        }
        return smsResultDto;
    }
}
